package org.origin.centre.support.lang;

import java.util.HashMap;
import java.util.Map;

/**
 * 扩展链式调用添加的 HashMap<K, V>
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
public class XMap<K, V> extends HashMap<K, V> {

    public XMap() {
        super();
    }

    public XMap(Map<? extends K, ? extends V> m) {
        super(m);
    }

    public XMap<K, V> of(K key, V value) {
        super.put(key, value);
        return this;
    }

    public XMap<K, V> of(Map<? extends K, ? extends V> map) {
        super.putAll(map);
        return this;
    }

    @SuppressWarnings("unchecked")
    public XMap<K, V> pushMap(Object object) {
        if (object != null) {
            if (object instanceof Map) {
                Map<K, V> map = (Map<K, V>) object;
                if (map.size() > 0) {
                    for (Map.Entry<K, V> entry : map.entrySet()) {
                        if (entry.getKey() != null) {
                            this.put(entry.getKey(), entry.getValue());
                        } else {
                            this.put(null, (V) entry.getValue());
                        }
                    }
                }
            }
        }
        return this;
    }

}
